/**
 * Created by zhourh on 2018/5/25.
 *
 * 给定一个Excel表格中的列名称，返回其相应的列序号。

 例如，

 A -> 1
 B -> 2
 C -> 3
 ...
 Z -> 26
 AA -> 27
 AB -> 28
 ...

 幂函数
 */
public class ExcelSheetColumnNumber {

    public static void main(String[] args) {
        ExcelSheetColumnNumber excelSheetColumnNumber = new ExcelSheetColumnNumber();
        System.out.println("A = " + excelSheetColumnNumber.titleToNumber("A"));
        System.out.println("B = " + excelSheetColumnNumber.titleToNumber("B"));
        System.out.println("C = " + excelSheetColumnNumber.titleToNumber("C"));
        System.out.println("AB = " + excelSheetColumnNumber.titleToNumber("AB"));
        System.out.println("ZY = " + excelSheetColumnNumber.titleToNumber("ZY"));
    }
    public int titleToNumber(String s) {
        if (s == null || s.isEmpty()) {
            return 0;
        }
        String reverseTitle = new StringBuilder(s).reverse().toString();

        int result = 0;
        for (int i = 0, length = s.length(); i < length; i++) {
            if (i != 0) {
                result += Math.pow(26, i) * (reverseTitle.charAt(i) - 'A' + 1) ;
            } else {
                result += reverseTitle.charAt(i) - 'A' + 1;
            }
        }

        return result;
    }
}
